package org.chen.service.impl;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.chen.model.Article;
import org.chen.model.elastic.CrawlerTask;
import org.chen.model.mongodb.ArticleDocument;
import org.chen.model.vo.CrawlerTaskVO;
import org.chen.model.vo.DashboardStatisticsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DashboardService {
    @Autowired
    private MongoTemplate mongoTemplate;

    public DashboardStatisticsVO getStatistics() {

        DashboardStatisticsVO vo = new DashboardStatisticsVO();
        
        // 获取统计数据
        DashboardStatisticsVO.Statistics statistics = getBasicStatistics();
        vo.setStatistics(statistics);
        
        // 获取最近文章
        List<DashboardStatisticsVO.ArticleVO> recentArticles = getRecentArticles();
        vo.setRecentArticles(recentArticles);
        
        // 获取爬虫任务
        List<CrawlerTaskVO> crawlerTasks = getRecentCrawlerTasks();
        vo.setCrawlerTasks(crawlerTasks);
        
        return vo;
    }
    
    private DashboardStatisticsVO.Statistics getBasicStatistics() {
        DashboardStatisticsVO.Statistics statistics = new DashboardStatisticsVO.Statistics();
        
        // 统计总文章数
        long totalArticles = mongoTemplate.count(new Query(), "articles");
        statistics.setTotalArticles(totalArticles);
        
        // 统计爬虫文章数
        Query crawlerQuery = new Query(Criteria.where("source").is("crawler"));
        long crawlerArticles = mongoTemplate.count(crawlerQuery, "articles");
        statistics.setCrawlerArticles(crawlerArticles);
        
        // 统计图片资源数（假设存储在images集合中）
        long totalImages = mongoTemplate.count(new Query(), "image_resources");
        statistics.setTotalImages(totalImages);
        
        // 统计爬虫任务数
        long crawlerTasks = mongoTemplate.count(new Query(), "crawler_tasks");
        statistics.setCrawlerTasks(crawlerTasks);
        
        return statistics;
    }
    
    private List<DashboardStatisticsVO.ArticleVO> getRecentArticles() {
        // 查询最近10篇文章
        Query query = new Query()
                .with(Sort.by(Sort.Direction.DESC, "createTime"))
                .limit(5);
                
        return mongoTemplate.find(query, ArticleDocument.class, "articles")
                .stream()
                .map(this::convertToArticleVO)
                .collect(Collectors.toList());
    }
    
    private DashboardStatisticsVO.ArticleVO convertToArticleVO(ArticleDocument article) {
        DashboardStatisticsVO.ArticleVO vo = new DashboardStatisticsVO.ArticleVO();
        vo.setId(article.getId());
        vo.setTitle(article.getTitle());
        vo.setType(article.getAuthor());
        vo.setCreateTime(article.getCreateTime());
        return vo;
    }
    
    private List<CrawlerTaskVO> getRecentCrawlerTasks() {
        // 查询最近的爬虫任务
        Query query = new Query()
                .with(Sort.by(Sort.Direction.DESC, "startTime"))
                .limit(5);
                
        return mongoTemplate.find(query, CrawlerTask.class, "crawler_tasks")
                .stream()
                .map(this::convertToCrawlerTaskVO)
                .collect(Collectors.toList());
    }

    private CrawlerTaskVO convertToCrawlerTaskVO(CrawlerTask task) {
        CrawlerTaskVO vo = new CrawlerTaskVO();
        BeanUtils.copyProperties(task, vo);
        return vo;
    }
}